import axios from 'axios';

const UserModule={
    state:{
        registForm:{
        },
        loginForm:{
        },
        warnMsg:"",
    },
    mutations:{
        registValue(state, list) {
            if (!list.account || !list.psw || !list.confirm|| !list.checked|| !list.captcha) {
                state.warnMsg = "请完善信息";
            } else if (list.psw != list.confirm) {
                state.warnMsg = "两次密码输入不一致";
            } else {
                state.warnMsg = "正在提交,请稍候";
            }
            layer.msg(state.warnMsg);
        },
        loginValue(state, list) {
            if (!list.account || !list.psw) {
                state.warnMsg = "请完善信息";
            }else {
                state.warnMsg = "正在提交,请稍候";
            }
            layer.msg(state.warnMsg);
        },
    },
    actions:{
        registSubmit({commit, state}) {
            var list = state.registForm;
            commit("registValue", list);
            if (state.warnMsg == "正在提交,请稍候") {
                var param = {
                    account: list.account,
                    psw: list.psw,
                    confirm: list.confirm
                }
                axios.post("/api/users/regist", param)
                .then(res => {
                    layer.msg(res.data.msg);
                })
            }
        },
        loginSubmit({commit, state}) {
            var list = state.loginForm;
            commit("loginValue", list);
            if (state.warnMsg == "正在提交,请稍候") {
                var param = {
                    account: list.account,
                    psw: list.psw,
                }
                axios.post("/api/users/login", param)
                .then(res => {
                    layer.msg(res.data.msg);
                    if(res.data.msg=="登录成功"){
                       window.location.href="http://127.0.0.1:8080";
                    }
                })
            }
        },
        
    },
}

export default UserModule;